package com.javacoo.junit.generator.internal.plugin.mock;

import java.util.Map;

import com.javacoo.junit.generator.enmus.TemplateTypeEnum;
import com.javacoo.junit.generator.internal.plugin.AbstractTemplatePlugin;
import com.javacoo.junit.generator.model.MethodHandleContext;

import static com.javacoo.junit.generator.internal.util.Formats.BLANK_4;
import static com.javacoo.junit.generator.internal.util.Formats.BLANK_8;
import static com.javacoo.junit.generator.internal.util.Formats.RT_1;

/**
 * mock模板插件默认实现
 * <li></li>
 * @author duanyong@jccfc.com
 * @date 2021/1/26 16:18
 */
public class MockTemplatePlugin extends AbstractTemplatePlugin {
    /**mock模板路径*/
    private static final String MOCK_PACKAGE = "Mock";
    /**
     * 根据类对象获取模板数据
     * <li></li>
     *
     * @param sourceClass :类对象
     * @author duanyong@jccfc.com
     * @date 2021/1/4 11:09
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @Override
    public Map<String, Object> getTemplateData(Class sourceClass) {
        Map<String, Object> data = buildCommonDataMap(sourceClass);


        return data;
    }

    /**
     * 获取模板路径
     * <li></li>
     *
     * @author duanyong@jccfc.com
     * @date 2021/1/8 10:57
     * @return: java.lang.String
     */
    @Override
    public String getTemplatePath() {
        return BASE_TEMPLATE_PACKAGE+ MOCK_PACKAGE;
    }

    /**
     * 获取模板名称
     * <li></li>
     *
     * @author duanyong@jccfc.com
     * @date 2021/1/5 11:41
     * @return: java.lang.String 模板名称
     */
    @Override
    public String getTemplateName() {
        return TemplateTypeEnum.TEMPLATE_TYPE_ENUM_DEFAULT.getValue();
    }

    @Override
    protected boolean needDefineVal() {
        return false;
    }

    @Override
    protected void handleMethodBodyAfter(MethodHandleContext methodHandleContext){
        if (methodHandleContext.isPrivate() || methodHandleContext.isStatic()) {
            return;
        }
        String blank = BLANK_8;
        if (methodHandleContext.getException() != null) {
            blank = BLANK_4 + blank;
        }
        methodHandleContext.getMethodBody().append(blank + "//TODO: 检查并修改生成的Mock代码" + RT_1);
        methodHandleContext.getMethodBody().append(blank + "//行为验证" + RT_1);
        methodHandleContext.getMethodBody().append(blank + "verify("+ methodHandleContext.getInstanceName() +")."+methodHandleContext.getMethodName()+ "(" + methodHandleContext.getInvokeParams() + ");" + RT_1);
    }
}
